/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.DanhMucDAO;
import dao.DanhMucNhaSanXuatDAO;
import dao.LoaiDauGiaDAO;
import dao.NhaSanXuatDAO;
import dao.SanphamDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Danhmuc;
import model.DanhmucNhasanxuat;
import model.Hinhanh;
import model.Loaidaugia;
import model.Nhasanxuat;
import model.Sanpham;

/**
 *
 * @author HoangLiem
 */
@WebServlet(name = "tungNhaSanXuat", urlPatterns = {"/tungNhaSanXuat"})
public class tungNhaSanXuat extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();

            String maDanhMuc = "";
            if (request.getParameter("maDanhMuc") != null) {
                maDanhMuc = request.getParameter("maDanhMuc");
            }

            String maNhaSanXuat = "";
            if (request.getParameter("maNhaSanXuat") != null) {
                maNhaSanXuat = request.getParameter("maNhaSanXuat");
            }

            int soSanPhamTrenTrang = -1;
            if (request.getParameter("soSanPhamTrenTrang") != null) {
                soSanPhamTrenTrang = Integer.parseInt(request.getParameter("soSanPhamTrenTrang"));
            }

            int trang = 1;
            if (request.getParameter("trang") != null) {
                trang = Integer.parseInt(request.getParameter("trang"));
            }
            int soTrang = 1;

            int maLoaiDauGia = -1;
            if (request.getParameter("maLoaiDauGia") != null) {
                maLoaiDauGia = Integer.parseInt(request.getParameter("maLoaiDauGia"));
            }
            float giaDau = -1;
            float giaCuoi = -1;
            if (request.getParameter("giaDau") != null) {
                giaDau = Float.parseFloat(request.getParameter("giaDau"));
            }
            if (request.getParameter("giaCuoi") != null) {
                giaCuoi = Float.parseFloat(request.getParameter("giaCuoi"));
            }
            
            Danhmuc dm = DanhMucDAO.getCategoryById(maDanhMuc);
            Loaidaugia loaiDauGia = LoaiDauGiaDAO.getABidType(maLoaiDauGia);

            Nhasanxuat nsx = NhaSanXuatDAO.getManufacturerById(maNhaSanXuat);
            DanhmucNhasanxuat dmNsx = DanhMucNhaSanXuatDAO.getACategory_Manufacturer(dm, nsx);

            ArrayList<Sanpham> dsTatCaSanPham = new ArrayList<Sanpham>();
            ArrayList<ArrayList<Hinhanh>> tatCaDaydsHinhAnh = new ArrayList<ArrayList<Hinhanh>>();//ds hinh anh tuong ứng
           if (maLoaiDauGia == -1) {
                if (giaDau == -1) {
                    dsTatCaSanPham = SanphamDAO.getProducts(dmNsx);
                } else {
                    dsTatCaSanPham = SanphamDAO.getProducts(dmNsx, giaDau, giaCuoi, 1);
                }
            } else {
                if (giaDau == -1) {
                    dsTatCaSanPham = SanphamDAO.getProducts(dmNsx, loaiDauGia);
                } else {
                    dsTatCaSanPham = SanphamDAO.getProducts(dmNsx, loaiDauGia, giaDau, giaCuoi, maLoaiDauGia);
                }
            }

            tatCaDaydsHinhAnh = SanphamDAO.getAllImages(dsTatCaSanPham);

            ArrayList<Sanpham> dsSanPham = new ArrayList<Sanpham>();
            ArrayList<ArrayList<Hinhanh>> DaydsHinhAnh = new ArrayList<ArrayList<Hinhanh>>();
            if (soSanPhamTrenTrang == -1) {
                soTrang = 1;
                dsSanPham = dsTatCaSanPham;
                DaydsHinhAnh = tatCaDaydsHinhAnh;
            } else {
                int soLuongSanPham = dsTatCaSanPham.size();
                soTrang = SanphamDAO.tinhSoLuongTrang(soLuongSanPham, soSanPhamTrenTrang);
                int batDau = (trang - 1) * soSanPhamTrenTrang;
                int ketThuc = trang * soSanPhamTrenTrang;
                if (ketThuc > soLuongSanPham) {
                    ketThuc = soLuongSanPham;
                }
                for (int i = batDau; i < ketThuc; i++) {
                    dsSanPham.add(dsTatCaSanPham.get(i));
                    DaydsHinhAnh.add(tatCaDaydsHinhAnh.get(i));
                }
            }
            request.setAttribute("maDanhMuc", maDanhMuc);
            request.setAttribute("maLoaiDauGia", maLoaiDauGia);
            request.setAttribute("maNhaSanXuat", maNhaSanXuat);
            request.setAttribute("giaDau", giaDau);
            request.setAttribute("giaCuoi", giaCuoi);
            request.setAttribute("soSpTrenTrang", soSanPhamTrenTrang);
            request.setAttribute("trang", trang);
            request.setAttribute("soTrang", soTrang);
            request.setAttribute("curDanhMuc", dm);
            request.setAttribute("curNhaSanXuat", nsx);
            request.setAttribute("dsSanPham", dsSanPham);
            request.setAttribute("DaydsHinhAnh", DaydsHinhAnh);

            String url = "tungnhasanxuatPage.jsp";
            session.setAttribute("urlback", "tungNhaSanXuat?maDanhMuc="+maDanhMuc+"&maNhaSanXuat=" + maNhaSanXuat);
            RequestDispatcher rd = request.getRequestDispatcher(url);
            rd.forward(request, response);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
